package cn.edu.sgu.www.mhxysy.service.change.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.change.BianshenkaCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.change.BianshenkaCategory;
import cn.edu.sgu.www.mhxysy.mapper.change.BianshenkaCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.change.BianshenkaCategoryPager;
import cn.edu.sgu.www.mhxysy.service.change.BianshenkaCategoryService;
import cn.edu.sgu.www.mhxysy.util.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class BianshenkaCategoryServiceImpl implements BianshenkaCategoryService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final BianshenkaCategoryMapper bianshenkaCategoryMapper;

    @Autowired
    public BianshenkaCategoryServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            BianshenkaCategoryMapper bianshenkaCategoryMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.bianshenkaCategoryMapper = bianshenkaCategoryMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_BIANSHENKA;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(BianshenkaCategoryDTO bianshenkaCategoryDTO) {
        BianshenkaCategory bianshenkaCategory = toEntity(bianshenkaCategoryDTO);

        bianshenkaCategory.setId(null);

        bianshenkaCategoryMapper.insert(bianshenkaCategory);
    }

    @Override
    public void updateById(BianshenkaCategoryDTO bianshenkaCategoryDTO) {
        ObjectUtils.requireNonNull(bianshenkaCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

        BianshenkaCategory bianshenkaCategory = toEntity(bianshenkaCategoryDTO);

        bianshenkaCategoryMapper.updateById(bianshenkaCategory);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<BianshenkaCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                bianshenkaCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public List<BianshenkaCategory> selectAll() {
        return bianshenkaCategoryMapper.selectList(null);
    }

    @Override
    public BianshenkaCategory selectById(Integer id) {
        return bianshenkaCategoryMapper.selectById(id);
    }

    @Override
    public Page<BianshenkaCategory> selectByPage(BianshenkaCategoryPager pager) {
        QueryWrapper<BianshenkaCategory> queryWrapper = new QueryWrapper<>();
        Page<BianshenkaCategory> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getType() != null,
                "type", pager.getType()
        );
        queryWrapper.like(
                StringUtils.isNotEmpty(pager.getName()),
                "name", pager.getName()
        );
        queryWrapper.orderByAsc("type", "grade");

        return bianshenkaCategoryMapper.selectPage(page, queryWrapper);
    }

    private BianshenkaCategory toEntity(BianshenkaCategoryDTO bianshenkaCategoryDTO) {
        BianshenkaCategory bianshenkaCategory = new BianshenkaCategory();

        bianshenkaCategory.setId(bianshenkaCategoryDTO.getId());
        bianshenkaCategory.setName(bianshenkaCategoryDTO.getName());
        bianshenkaCategory.setType(bianshenkaCategoryDTO.getType());
        bianshenkaCategory.setGrade(bianshenkaCategoryDTO.getGrade());

        return bianshenkaCategory;
    }

}